package com.tansuo365.test1.service.kucunservice.kucunimpl;

import com.tansuo365.test1.bean.kucunbean.ChukuZhixing;
import com.tansuo365.test1.bean.kucunbean.TuikuDetails;
import com.tansuo365.test1.bean.kucunbean.TuikuZong;
import com.tansuo365.test1.bean.kucunbean.WuliaoKucunRiZhi;
import com.tansuo365.test1.bean.otherbean.HuiShouZhixing;
import com.tansuo365.test1.mapper.kucunmapper.*;
import com.tansuo365.test1.mapper.othermapper.HuiShouZhixingMapper;
import com.tansuo365.test1.service.kucunservice.TuikuZongService;
import com.tansuo365.test1.service.otherservice.HuishouZhixingService;
import org.springframework.util.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TuikuZongServiceImpl implements TuikuZongService {

    @Resource
    private TuikuZongMapper tuikuZongMapper;
    @Resource
    private TuikuDetailsMapper tuikuDetailsMapper;
    @Resource
    private WuliaoKucunSearMapper wuliaoKucunSearMapper;
    @Resource
    private WuliaoKucunRiZhiMapper wuliaoKucunRiZhiMapper;
    @Resource
    private HuiShouZhixingMapper huiShouZhixingMapper;
    @Resource
    private ChukuZongMapper chukuZongMapper;
    @Resource
    private ChukuZhixingMapper chukuZhixingMapper;

    @Override
    public String getTodayMaxTuikuDanNumber() {
        return tuikuZongMapper.getTodayMaxTuikuDanNumber();
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return tuikuZongMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insertSelective(TuikuZong tuikuZong) {
        return tuikuZongMapper.insertSelective(tuikuZong);
    }

    @Override
    public int deleteBatchByPKArr(Integer[] ids) {
        return tuikuZongMapper.deleteBatchByPKArr(ids);
    }

    @Override
    public TuikuZong selectByPrimaryKey(Integer id) {
        return tuikuZongMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(TuikuZong tuikuZong) {
        return tuikuZongMapper.updateByPrimaryKeySelective(tuikuZong);
    }

    @Override
    @Transactional
    public int saveZongAndDetails(TuikuZong tuikuZong, List<TuikuDetails> tuikuDetailsList) {
        int insertCode = 0;
        insertCode = tuikuZongMapper.insertSelective(tuikuZong);
        if (!(insertCode > 0)) {
            return 0;
        }
        Integer zongzhangId = tuikuZong.getId();
        int i = 0;
        int upKu = 0;
        int inSertRizhi = 0;
        int inDetails = 0;
        //保存每个退库单物料
        String duiyingchukudanhao = "";
        for (TuikuDetails tuikuDetails : tuikuDetailsList) {
            //设置对应总表id
            i++;
            if (duiyingchukudanhao == "") {
                duiyingchukudanhao = tuikuDetails.getDuiyingchukudanhao();
            }
            tuikuDetails.setZongzhangid(zongzhangId);
            inDetails += tuikuDetailsMapper.insertSelective(tuikuDetails);
//            upKu += wuliaoKucunSearMapper.updateWuliaoTuihuiShuliangByWuliaoName(tuikuDetails.getMingchengguige(), tuikuDetails.getTuihuishuliang(), tuikuDetails.getRukukufang());
            //TODO 将每个保存的详细insert到物料日志表
            WuliaoKucunRiZhi wuliaoKucunRiZhi = new WuliaoKucunRiZhi();
            wuliaoKucunRiZhi.setMingchengguige(tuikuDetails.getMingchengguige());
            wuliaoKucunRiZhi.setXiangmuweihu(tuikuZong.getXiangmumingcheng());
            wuliaoKucunRiZhi.setTuihuishuliang(tuikuDetails.getTuihuishuliang());
            wuliaoKucunRiZhi.setDanwei(tuikuDetails.getDanwei());
            wuliaoKucunRiZhi.setRukukufang(tuikuDetails.getRukukufang());
            wuliaoKucunRiZhi.setWuliaobianma(tuikuDetails.getWuliaobianma());
            wuliaoKucunRiZhi.setDalei(tuikuDetails.getDalei());
            inSertRizhi += wuliaoKucunRiZhiMapper.insertSelective(wuliaoKucunRiZhi);
        }
        if (!(i == tuikuDetailsList.size() && i != 0)) {
            return 0;
        } else if (!(i == inDetails)) {
            //update的退库数和条目不一样多,那么就说明有的物料没执行update成功,或者是因为库存没有这个物料
            return 0;
        }
        if (!(inSertRizhi == tuikuDetailsList.size())) {
            return 0;
        }
        int i1 = chukuZongMapper.updateCandeleteToOne(duiyingchukudanhao);
        if(i1>0){
            return i;//返回的是插入退库详情物料的数量
        }else{
            return -3;
        }
    }

    @Override
    public List<TuikuZong> listTuikuZong(TuikuZong tuikuZong) {
        return tuikuZongMapper.listTuikuZong(tuikuZong);
    }

    @Override
    @Transactional
    public int deleteTuikuBindChuku(Integer[] ids) {
        int changeCode = 0;
        int tuikulikehuishouUpCode = 0;
        for (Integer id : ids) {
            //根据到货单id总 获取到货详细子表(获取子表中的数据)
            List<TuikuDetails> tuikuDetailsList = tuikuDetailsMapper.listTuikuDetailsByZongId(id);
            for (TuikuDetails tuikuDetails : tuikuDetailsList) {

                //根据对应出库单号,名称规格 来获取出库执行中这条数据
                ChukuZhixing chukuZhixing = chukuZhixingMapper.listChukuZhixingByDanhaoAndMingcheng(tuikuDetails.getDuiyingchukudanhao(), tuikuDetails.getMingchengguige());
                //根据退库明细里的对应出库单号和名称规格,对比回收汇总里是否有相同的数据,有则将退库明细里该条的退库数量从回收汇总里对应的这条中减去
                HuiShouZhixing tuikulikehuishou = huiShouZhixingMapper.getHuishouZhixingByCkdhAndName(
                        tuikuDetails.getDuiyingchukudanhao(), tuikuDetails.getMingchengguige());

//                huiShouZhixingMapper.de
                String chukutuikuzhuangtai = "";
                double preErrorKey = 1e-6; //1*10的-6次方即0.000001
                double key1 = 0.0000001d;
                double key2 = 0d;
                if (tuikulikehuishou != null) {
                    Double chukuhuishoushuliang = tuikulikehuishou.getChukushuliang();
                    System.out.println("chukushu:" + chukuhuishoushuliang);
                    Double tuikushuliang = tuikulikehuishou.getTuikushuliang();
                    System.out.println("tuikushuliang_yuan:" + tuikushuliang);
                    Double yihuishoushuliang = tuikulikehuishou.getHuishoushuliang();
                    System.out.println("yihuishoushuliang:" + yihuishoushuliang);
                    Double tuihuishuliangindetails = tuikuDetails.getTuihuishuliang();
                    System.out.println("tuihuishuliangindetails:" + tuihuishuliangindetails);
                    Double tuikushuliang_now = tuikushuliang - tuihuishuliangindetails;
                    System.out.println("tuikushuliang_now:" + tuikushuliang_now);
                    Double weihuishoushuliang =
                            chukuhuishoushuliang - tuikushuliang_now - yihuishoushuliang;
                    System.out.println("weihuishoushuliang:" + weihuishoushuliang);

                    String huishouzhuangtai = "";
                    if (Math.abs(chukuhuishoushuliang - tuikushuliang_now - yihuishoushuliang) < preErrorKey && (chukuhuishoushuliang > 0)) {
                        huishouzhuangtai = "全部回收";
                    } else if ((yihuishoushuliang + tuikushuliang_now < preErrorKey) && (chukuhuishoushuliang != 0)) {
                        huishouzhuangtai = "未回收";
                    } else if (((tuikushuliang_now + yihuishoushuliang) < chukuhuishoushuliang) && yihuishoushuliang > 0) {
                        huishouzhuangtai = "部分回收";
                    } else if (((tuikushuliang_now + yihuishoushuliang) < chukuhuishoushuliang) && tuikushuliang_now > 0) {
                        huishouzhuangtai = "部分退库";
                    } else if ((tuikushuliang_now + yihuishoushuliang) > chukuhuishoushuliang) {
                        huishouzhuangtai = "超量回收";
                    }
                    System.out.println("huishouzhuangtai:" + huishouzhuangtai);
                    tuikulikehuishou.setHuishouzhuangtai(huishouzhuangtai);
                    tuikulikehuishou.setId(tuikulikehuishou.getId());
                    tuikulikehuishou.setTuikushuliang(tuikushuliang_now);
                    tuikulikehuishou.setWeihuishoushuliang(weihuishoushuliang);
                    int i1 = huiShouZhixingMapper.updateZhixingChangeTuikuWeihuishouAndZhuangtaiOnDelete(tuikulikehuishou);
                }

                Double chukushuliang = chukuZhixing.getChukushuliang();//申购数量
                Double chukushuliangZhixing = chukuZhixing.getTuikushuliang();//申购数量
                Double tuihuishuliang = tuikuDetails.getTuihuishuliang();//到货数量
                Double weituihuiliang = chukushuliang - tuihuishuliang;
                if (Math.abs((chukushuliangZhixing - tuikuDetails.getTuihuishuliang()) - chukushuliang) < preErrorKey && (weituihuiliang == 0)) {
                    chukutuikuzhuangtai = "全部退回";
                } else if ((chukushuliangZhixing - tuikuDetails.getTuihuishuliang() == 0) && (chukushuliang != 0)) {
                    chukutuikuzhuangtai = "未退回";
                } else if ((chukushuliangZhixing - tuikuDetails.getTuihuishuliang() < chukushuliang) && tuihuishuliang > 0) {
                    chukutuikuzhuangtai = "部分退回";
                } else if (chukushuliangZhixing - tuikuDetails.getTuihuishuliang() > chukushuliang) {
                    chukutuikuzhuangtai = "超量退回";
                }
                chukuZhixing.setTuikushuliang(-tuihuishuliang); //注意是-的
                chukuZhixing.setChukutuikuzhuangtai(chukutuikuzhuangtai);//重新设定出库执行汇总状态
                chukuZhixing.setId(chukuZhixing.getId());

                int i = chukuZhixingMapper.updateZhixingChangeTuikuWeiTuikuAndZhuangtai(chukuZhixing);
                changeCode += i;
            }
        }
        if (changeCode > 0) {
            return changeCode;
        } else {
            return 0;
        }
    }
}
